<!doctype html>
<html lang="zh-CN">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="referrer" content="no-referrer-when-downgrade">
    

    <title>kubernetes-client之python详细接口调用 | Sang的博客</title>
    <meta property="og:title" content="kubernetes-client之python详细接口调用 - Sang的博客">
    <meta property="og:type" content="article">
        
    <meta property="article:published_time" content='2021-11-23T11:32:18&#43;08:00'>
        
        
    <meta property="article:modified_time" content='2021-11-23T11:32:18&#43;08:00'>
        
    <meta name="Keywords" content="golang,go语言,shell,k8s,博客,python,软件架构,公众号">
    <meta name="description" content="kubernetes-client之python详细接口调用">
        
    <meta name="author" content="仨哥">
    <meta property="og:url" content="https://stto_32.gitee.io/site/post/kubernetes-client%E4%B9%8Bpython%E8%AF%A6%E7%BB%86%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/">
    <link rel="shortcut icon" href='/site/favicon.ico'  type="image/x-icon">

    <link rel="stylesheet" href='/site/css/normalize.css'>
    <link rel="stylesheet" href='/site/css/style.css'>
    <script type="text/javascript" src="//cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    
    
    
        <link href="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" rel="stylesheet">
    
    
    
    
        <link rel="stylesheet" href='/site/css/douban.css'>
    
        <link rel="stylesheet" href='/site/css/other.css'>
    
</head>


<body>
    <header id="header" class="clearfix">
    <div class="container">
        <div class="col-group">
            <div class="site-name ">
                
                    <a id="logo" href="https://stto_32.gitee.io/site">
                        Sang的博客
                    </a>
                
                <p class="description">专注于Python、Go语言(golang)、云原生</p>
            </div>
            <div>
                <nav id="nav-menu" class="clearfix">
                    <a class="current" href="https://stto_32.gitee.io/site">首页</a>
                    
                    <a  href="https://stto_32.gitee.io/site/tools/" title="小工具">小工具</a>
                    
                    <a  href="https://stto_32.gitee.io/site/archives/" title="归档">归档</a>
                    
                    <a  href="https://stto_32.gitee.io/site/about/" title="关于">关于</a>
                    
                </nav>
            </div>
        </div>
    </div>
</header>

    <div id="body">
        <div class="container">
            <div class="col-group">

                <div class="col-8" id="main">
                    
<div class="res-cons">
    
    <article class="post">
        <header>
            <h1 class="post-title">kubernetes-client之python详细接口调用</h1>
        </header>
        <date class="post-meta meta-date">
            2021年11月23日
        </date>
        
        
        <div class="post-meta">
            <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                    阅读</span></span>
        </div>
        
        
        <div class="post-content">
            <p>kubernetes-client之python详细接口调用</p>
<blockquote>
<p>先上<a href="https://github.com/kubernetes-client/python">链接</a></p>
</blockquote>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
<table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  1
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  2
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  3
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  4
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  5
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  6
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  7
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  8
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">  9
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 10
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 11
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 12
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 13
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 14
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 15
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 16
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 17
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 18
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 19
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 20
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 21
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 22
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 23
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 24
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 25
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 26
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 27
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 28
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 29
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 30
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 31
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 32
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 33
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 34
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 35
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 36
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 37
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 38
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 39
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 40
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 41
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 42
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 43
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 44
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 45
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 46
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 47
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 48
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 49
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 50
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 51
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 52
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 53
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 54
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 55
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 56
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 57
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 58
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 59
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 60
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 61
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 62
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 63
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 64
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 65
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 66
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 67
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 68
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 69
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 70
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 71
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 72
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 73
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 74
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 75
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 76
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 77
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 78
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 79
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 80
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 81
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 82
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 83
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 84
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 85
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 86
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 87
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 88
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 89
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 90
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 91
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 92
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 93
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 94
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 95
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 96
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 97
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 98
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 99
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">100
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">101
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">102
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">103
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">104
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">105
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">106
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">107
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">108
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">109
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">110
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">111
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">112
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">113
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">114
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">115
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">116
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">117
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">118
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">119
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">120
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">121
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">122
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">123
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">124
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">125
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">126
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">127
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">128
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">129
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">130
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">131
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">132
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">133
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">134
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">135
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">136
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">137
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">138
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">139
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">140
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">141
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">142
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">143
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">144
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">145
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">146
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">147
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">148
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">149
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">150
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">151
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">152
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">153
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">154
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">155
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">156
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">157
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">158
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">159
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">160
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">161
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">162
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">163
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">164
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">165
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">166
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">167
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">168
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">169
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">170
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">171
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">172
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">173
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">174
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">175
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">176
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">177
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">178
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">179
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">180
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">181
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">182
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">183
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">184
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">185
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">186
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">187
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">188
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">189
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">190
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">191
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">192
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">193
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">194
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">195
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">196
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">197
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">198
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">199
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">200
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">201
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">202
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">203
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">204
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">205
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">206
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">207
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">208
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">209
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">210
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">211
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">212
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">213
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">214
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">215
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">216
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">217
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">218
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">219
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">220
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">221
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">222
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">223
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">224
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">225
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">226
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">227
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">228
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">229
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">230
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">231
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">232
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">233
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">234
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">235
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">236
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">237
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">238
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">239
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">240
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">241
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">242
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">243
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">244
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">245
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">246
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">247
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">248
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">249
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">250
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">251
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">252
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">253
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">254
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">255
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">256
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">257
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">258
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">259
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">260
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">261
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">262
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">263
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">264
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">265
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">266
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">267
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">268
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">269
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">270
</span><span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">271
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">
<span style="color:#75715e">#!/usr/bin/env python</span>
<span style="color:#75715e"># -*- coding:utf-8 -*-</span>
<span style="color:#75715e"># @Time    : 2021/12/1 17:07</span>
<span style="color:#75715e"># @Author  : XuLiLiang</span>
<span style="color:#75715e"># @Email   : xuliliang@epailive.com</span>
<span style="color:#75715e"># @File    : k8s-demo.py</span>
<span style="color:#75715e"># @Description : 操作k8s api</span>


<span style="color:#f92672">import</span> urllib3
<span style="color:#f92672">import</span> yaml
<span style="color:#f92672">from</span> kubernetes <span style="color:#f92672">import</span> client
<span style="color:#f92672">from</span> kubernetes.client <span style="color:#f92672">import</span> ApiException

urllib3<span style="color:#f92672">.</span>disable_warnings()

<span style="color:#e6db74">&#39;&#39;&#39;步骤
</span><span style="color:#e6db74">
</span><span style="color:#e6db74">先pip install kubernetes
</span><span style="color:#e6db74">1、创建用户
</span><span style="color:#e6db74">vi CreateServiceAccount.yaml
</span><span style="color:#e6db74">apiVersion: v1
</span><span style="color:#e6db74">kind: ServiceAccount
</span><span style="color:#e6db74">metadata:
</span><span style="color:#e6db74">  name: admin-user
</span><span style="color:#e6db74">  namespace: kube-system
</span><span style="color:#e6db74">kubectl create -f CreateServiceAccount.yaml
</span><span style="color:#e6db74">
</span><span style="color:#e6db74">2、授权
</span><span style="color:#e6db74">kind: ClusterRoleBinding
</span><span style="color:#e6db74">apiVersion: rbac.authorization.k8s.io/v1
</span><span style="color:#e6db74">metadata:
</span><span style="color:#e6db74">  name: admin-user
</span><span style="color:#e6db74">subjects:
</span><span style="color:#e6db74">- kind: ServiceAccount
</span><span style="color:#e6db74">  name: admin-user
</span><span style="color:#e6db74">  namespace: kube-system
</span><span style="color:#e6db74">roleRef:
</span><span style="color:#e6db74">  kind: ClusterRole
</span><span style="color:#e6db74">  name: cluster-admin
</span><span style="color:#e6db74">  apiGroup: rbac.authorization.k8s.io
</span><span style="color:#e6db74">3、获取token
</span><span style="color:#e6db74">kubectl describe secret $(kubectl get secret -n kube-system | grep ^admin-user | awk &#39;{print $1}&#39;) -n kube-system | grep -E &#39;^token&#39;| awk &#39;{print $2}&#39;
</span><span style="color:#e6db74">
</span><span style="color:#e6db74">
</span><span style="color:#e6db74">&#39;&#39;&#39;</span>

apiToken <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;</span>
host <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;https://100.100.100.5:6443&#39;</span>


<span style="color:#75715e"># deployment_template = {</span>
<span style="color:#75715e">#     &#34;apiVersion&#34;: &#34;apps/v1&#34;,</span>
<span style="color:#75715e">#     &#34;kind&#34;: &#34;Deployment&#34;,</span>
<span style="color:#75715e">#     &#34;metadata&#34;: {</span>
<span style="color:#75715e">#         &#34;name&#34;: &#34;myapp&#34;,</span>
<span style="color:#75715e">#         &#34;namespace&#34;: &#34;&#34;,</span>
<span style="color:#75715e">#         &#34;labels&#34;: {</span>
<span style="color:#75715e">#             &#34;app&#34;: &#34;myapp&#34;</span>
<span style="color:#75715e">#         }</span>
<span style="color:#75715e">#     },</span>
<span style="color:#75715e">#     &#34;spec&#34;: {</span>
<span style="color:#75715e">#         &#34;replicas&#34;: 1,</span>
<span style="color:#75715e">#         &#34;selector&#34;: {</span>
<span style="color:#75715e">#             &#34;matchLabels&#34;: {</span>
<span style="color:#75715e">#                 &#34;app&#34;: &#34;myapp&#34;</span>
<span style="color:#75715e">#             }</span>
<span style="color:#75715e">#         },</span>
<span style="color:#75715e">#         &#34;template&#34;: {</span>
<span style="color:#75715e">#             &#34;metadata&#34;: {</span>
<span style="color:#75715e">#                 &#34;name&#34;: &#34;myapp-pod&#34;,</span>
<span style="color:#75715e">#                 &#34;labels&#34;: {</span>
<span style="color:#75715e">#                     &#34;app&#34;: &#34;myapp&#34;</span>
<span style="color:#75715e">#                 }</span>
<span style="color:#75715e">#             },</span>
<span style="color:#75715e">#             &#34;spec&#34;: {</span>
<span style="color:#75715e">#                 &#34;containers&#34;: [</span>
<span style="color:#75715e">#                     {</span>
<span style="color:#75715e">#                         &#34;name&#34;: &#34;myapp&#34;,</span>
<span style="color:#75715e">#                         &#34;image&#34;: &#34;1450793561/myapp:v1&#34;,</span>
<span style="color:#75715e">#                         &#34;ports&#34;: [</span>
<span style="color:#75715e">#                             {</span>
<span style="color:#75715e">#                                 &#34;name&#34;: &#34;http&#34;,</span>
<span style="color:#75715e">#                                 &#34;containerPort&#34;: 80</span>
<span style="color:#75715e">#                             }</span>
<span style="color:#75715e">#                         ]</span>
<span style="color:#75715e">#                     }</span>
<span style="color:#75715e">#                 ]</span>
<span style="color:#75715e">#             }</span>
<span style="color:#75715e">#         }</span>
<span style="color:#75715e">#     }</span>
<span style="color:#75715e"># }</span>
<span style="color:#75715e"># read = yaml.safe_dump(deployment_template)</span>


<span style="color:#75715e"># 模板----deployment</span>

<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">create_deployment_object</span>(deployment_name, namespace, container_name, container_image):
    container <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>V1Container(
        name<span style="color:#f92672">=</span>container_name,
        image<span style="color:#f92672">=</span>container_image,
        ports<span style="color:#f92672">=</span>[client<span style="color:#f92672">.</span>V1ContainerPort(container_port<span style="color:#f92672">=</span><span style="color:#ae81ff">80</span>)],
        resources<span style="color:#f92672">=</span>client<span style="color:#f92672">.</span>V1ResourceRequirements(requests<span style="color:#f92672">=</span>{<span style="color:#e6db74">&#34;cpu&#34;</span>: <span style="color:#e6db74">&#34;100m&#34;</span>, <span style="color:#e6db74">&#34;memory&#34;</span>: <span style="color:#e6db74">&#34;200Mi&#34;</span>},
                                                limits<span style="color:#f92672">=</span>{<span style="color:#e6db74">&#34;cpu&#34;</span>: <span style="color:#e6db74">&#34;500m&#34;</span>, <span style="color:#e6db74">&#34;memory&#34;</span>: <span style="color:#e6db74">&#34;500Mi&#34;</span>}))

    template <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>V1PodTemplateSpec(
        metadata<span style="color:#f92672">=</span>client<span style="color:#f92672">.</span>V1ObjectMeta(labels<span style="color:#f92672">=</span>{<span style="color:#e6db74">&#34;app&#34;</span>: container_name}),
        spec<span style="color:#f92672">=</span>client<span style="color:#f92672">.</span>V1PodSpec(containers<span style="color:#f92672">=</span>[container]),
    )
    spec <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>V1DeploymentSpec(
        replicas<span style="color:#f92672">=</span><span style="color:#ae81ff">3</span>,
        selector<span style="color:#f92672">=</span>{<span style="color:#e6db74">&#34;matchLabels&#34;</span>: {<span style="color:#e6db74">&#34;app&#34;</span>: container_name}, <span style="color:#e6db74">&#34;namespace&#34;</span>: namespace},
        template<span style="color:#f92672">=</span>template
    )
    deployment_temp <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>V1Deployment(
        api_version<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;apps/v1&#34;</span>,
        kind<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;Deployment&#34;</span>,
        metadata<span style="color:#f92672">=</span>client<span style="color:#f92672">.</span>V1ObjectMeta(name<span style="color:#f92672">=</span>deployment_name),
        spec<span style="color:#f92672">=</span>spec,
    )
    <span style="color:#66d9ef">return</span> deployment_temp


<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">K8sApi</span>:
    <span style="color:#66d9ef">def</span> __init__(self):
        self<span style="color:#f92672">.</span>apiToken <span style="color:#f92672">=</span> apiToken
        self<span style="color:#f92672">.</span>host <span style="color:#f92672">=</span> host
        configuration <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>Configuration()
        setattr(configuration, <span style="color:#e6db74">&#39;verify_ssl&#39;</span>, <span style="color:#66d9ef">False</span>)
        client<span style="color:#f92672">.</span>Configuration<span style="color:#f92672">.</span>set_default(configuration)
        configuration<span style="color:#f92672">.</span>host <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>host
        configuration<span style="color:#f92672">.</span>verify_ssl <span style="color:#f92672">=</span> <span style="color:#66d9ef">False</span>
        configuration<span style="color:#f92672">.</span>debug <span style="color:#f92672">=</span> <span style="color:#66d9ef">False</span>
        configuration<span style="color:#f92672">.</span>api_key <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#34;authorization&#34;</span>: <span style="color:#e6db74">&#34;Bearer &#34;</span> <span style="color:#f92672">+</span> self<span style="color:#f92672">.</span>apiToken}
        client<span style="color:#f92672">.</span>Configuration<span style="color:#f92672">.</span>set_default(configuration)
        self<span style="color:#f92672">.</span>core_api_v1 <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>CoreV1Api(client<span style="color:#f92672">.</span>ApiClient(configuration))
        self<span style="color:#f92672">.</span>apps_v1_api <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>AppsV1Api(client<span style="color:#f92672">.</span>ApiClient(configuration))

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">create_namespace</span>(self, ns_name):
        list_names <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>list_namespaces()
        <span style="color:#66d9ef">if</span> ns_name <span style="color:#f92672">not</span> <span style="color:#f92672">in</span> list_names:
            body <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;apiVersion&#39;</span>: <span style="color:#e6db74">&#39;v1&#39;</span>, <span style="color:#e6db74">&#39;kind&#39;</span>: <span style="color:#e6db74">&#39;Namespace&#39;</span>, <span style="color:#e6db74">&#39;metadata&#39;</span>: {<span style="color:#e6db74">&#39;name&#39;</span>: ns_name, <span style="color:#e6db74">&#39;labels&#39;</span>: {<span style="color:#e6db74">&#39;name&#39;</span>: ns_name}}}
            <span style="color:#66d9ef">try</span>:
                self<span style="color:#f92672">.</span>core_api_v1<span style="color:#f92672">.</span>create_namespace(body)
            <span style="color:#66d9ef">except</span> ApiException <span style="color:#66d9ef">as</span> e:
                print(e)
        <span style="color:#66d9ef">else</span>:
            print(<span style="color:#e6db74">f</span><span style="color:#e6db74">&#34;已经存在namespace:</span><span style="color:#e6db74">{</span>ns_name<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span>)

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">list_namespaces</span>(self):
        list_temp <span style="color:#f92672">=</span> []
        <span style="color:#66d9ef">try</span>:
            ret <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>core_api_v1<span style="color:#f92672">.</span>list_namespace(limit<span style="color:#f92672">=</span><span style="color:#ae81ff">50</span>, timeout_seconds<span style="color:#f92672">=</span><span style="color:#ae81ff">60</span>, watch<span style="color:#f92672">=</span><span style="color:#66d9ef">False</span>)
            <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> ret<span style="color:#f92672">.</span>items:
                list_temp<span style="color:#f92672">.</span>append(i<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>name)
            <span style="color:#66d9ef">return</span> list_temp
        <span style="color:#66d9ef">except</span> ApiException <span style="color:#66d9ef">as</span> e:
            print(<span style="color:#e6db74">&#34;Exception when calling CoreV1Api-&gt;list_namespace: </span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span> <span style="color:#f92672">%</span> e)

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">list_services</span>(self, ns):
        lit <span style="color:#f92672">=</span> []
        <span style="color:#66d9ef">try</span>:
            ret <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>core_api_v1<span style="color:#f92672">.</span>list_namespaced_service(namespace<span style="color:#f92672">=</span>ns)
            <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> ret<span style="color:#f92672">.</span>items:
                lit<span style="color:#f92672">.</span>append(i<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>name)
            <span style="color:#66d9ef">return</span> lit
        <span style="color:#66d9ef">except</span> ApiException <span style="color:#66d9ef">as</span> e:
            print(e)

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">list_pods</span>(self, ns):
        lit <span style="color:#f92672">=</span> []
        <span style="color:#66d9ef">try</span>:
            ret <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>core_api_v1<span style="color:#f92672">.</span>list_namespaced_pod(namespace<span style="color:#f92672">=</span>ns)
            <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> ret<span style="color:#f92672">.</span>items:
                lit<span style="color:#f92672">.</span>append(i<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>name)
                print(<span style="color:#e6db74">&#34;</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#e6db74">&#34;</span> <span style="color:#f92672">%</span> (i<span style="color:#f92672">.</span>status<span style="color:#f92672">.</span>pod_ip, i<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>namespace, i<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>name))
            <span style="color:#66d9ef">return</span> lit
        <span style="color:#66d9ef">except</span> ApiException <span style="color:#66d9ef">as</span> e:
            print(e)

    <span style="color:#75715e"># def create_namespace_deployment(self):</span>
    <span style="color:#75715e">#     with open(&#34;./test-pod.yaml&#34;) as f:</span>
    <span style="color:#75715e">#         body = yaml.safe_load(f)</span>
    <span style="color:#75715e">#         body[&#39;metadata&#39;][&#39;name&#39;] = &#34;testpod&#34;</span>
    <span style="color:#75715e">#         body[&#39;metadata&#39;][&#39;namespace&#39;] = &#34;test123&#34;</span>
    <span style="color:#75715e">#         try:</span>
    <span style="color:#75715e">#             self.apps_v1_api.create_namespaced_deployment(&#34;test123&#34;, body=body)</span>
    <span style="color:#75715e">#         except ApiException as e:</span>
    <span style="color:#75715e">#             print(&#34;失败&#34;, e)</span>

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">update_namespace_deployment</span>(self, name, namespace, new_image):
        body <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>apps_v1_api<span style="color:#f92672">.</span>read_namespaced_deployment(name, namespace)
        body<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>template<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>containers[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">.</span>image <span style="color:#f92672">=</span> new_image
        print(<span style="color:#e6db74">&#34;new images-------&gt;&gt;&gt;&gt;&gt;&#34;</span>, body<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>template<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>containers[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">.</span>image)
        <span style="color:#66d9ef">try</span>:
            resp<span style="color:#f92672">=</span>self<span style="color:#f92672">.</span>apps_v1_api<span style="color:#f92672">.</span>patch_namespaced_deployment(name, namespace, body)
            print(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">[INFO] deployment&#39;s container image updated.</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>)
            print(<span style="color:#e6db74">&#34;</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t\t\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#e6db74">&#34;</span> <span style="color:#f92672">%</span> (<span style="color:#e6db74">&#34;NAMESPACE&#34;</span>, <span style="color:#e6db74">&#34;NAME&#34;</span>, <span style="color:#e6db74">&#34;REVISION&#34;</span>, <span style="color:#e6db74">&#34;IMAGE&#34;</span>))
            print(
                <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>
                <span style="color:#f92672">%</span> (
                    resp<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>namespace,
                    resp<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>name,
                    resp<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>generation,
                    resp<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>template<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>containers[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">.</span>image,
                )
            )
        <span style="color:#66d9ef">except</span> ApiException <span style="color:#66d9ef">as</span> e:
            print(e)

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get_nodes_info</span>(self):
        response <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>core_api_v1<span style="color:#f92672">.</span>list_node()
        print(response<span style="color:#f92672">.</span>items)

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">create_dep</span>(self, deployments, deployment_name, namespace):

        <span style="color:#66d9ef">if</span> deployment_name <span style="color:#f92672">not</span> <span style="color:#f92672">in</span> self<span style="color:#f92672">.</span>list_deployments(namespace):
            resp <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>apps_v1_api<span style="color:#f92672">.</span>create_namespaced_deployment(
                body<span style="color:#f92672">=</span>deployments, namespace<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;test123123&#34;</span>
            )
            print(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">[INFO] deployment `nginx-deployment` created.</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>)
            print(<span style="color:#e6db74">&#34;</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t\t\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#e6db74">&#34;</span> <span style="color:#f92672">%</span> (<span style="color:#e6db74">&#34;NAMESPACE&#34;</span>, <span style="color:#e6db74">&#34;NAME&#34;</span>, <span style="color:#e6db74">&#34;REVISION&#34;</span>, <span style="color:#e6db74">&#34;IMAGE&#34;</span>))
            print(
                <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\t\t</span><span style="color:#e6db74">%s</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>
                <span style="color:#f92672">%</span> (
                    resp<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>namespace,
                    resp<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>name,
                    resp<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>generation,
                    resp<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>template<span style="color:#f92672">.</span>spec<span style="color:#f92672">.</span>containers[<span style="color:#ae81ff">0</span>]<span style="color:#f92672">.</span>image,
                )
            )
        <span style="color:#66d9ef">else</span>:
            print(<span style="color:#e6db74">&#34;deployment has exist&#34;</span>)

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">list_deployments</span>(self, namespace):
        list_temp <span style="color:#f92672">=</span> []
        deployments <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>apps_v1_api<span style="color:#f92672">.</span>list_namespaced_deployment(namespace)
        <span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> deployments<span style="color:#f92672">.</span>items:
            list_temp<span style="color:#f92672">.</span>append(i<span style="color:#f92672">.</span>metadata<span style="color:#f92672">.</span>name)
        <span style="color:#66d9ef">return</span> list_temp

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">delete_deployment</span>(self, deployment_name, namespace):

        <span style="color:#66d9ef">if</span> deployment_name <span style="color:#f92672">in</span> self<span style="color:#f92672">.</span>list_deployments(namespace):
            <span style="color:#66d9ef">try</span>:
                resp <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>apps_v1_api<span style="color:#f92672">.</span>delete_namespaced_deployment(
                    name<span style="color:#f92672">=</span>deployment_name,
                    namespace<span style="color:#f92672">=</span>namespace,
                    body<span style="color:#f92672">=</span>client<span style="color:#f92672">.</span>V1DeleteOptions(
                        propagation_policy<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;Foreground&#34;</span>, grace_period_seconds<span style="color:#f92672">=</span><span style="color:#ae81ff">5</span>
                    ),
                )
                print(resp)
                print(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">[INFO] deployment `nginx-deployment` deleted.&#34;</span>)
            <span style="color:#66d9ef">except</span> ApiException <span style="color:#66d9ef">as</span> e:
                print(e)
        <span style="color:#66d9ef">else</span>:
            print(<span style="color:#e6db74">&#34;deployment not exist &#34;</span>)


<span style="color:#66d9ef">if</span> __name__ <span style="color:#f92672">==</span> <span style="color:#e6db74">&#39;__main__&#39;</span>:
    <span style="color:#75715e"># api = K8sApi()</span>
    <span style="color:#75715e">#     # api.create_namespace_deployment()</span>
    <span style="color:#75715e">#     # api.replace_namespace_deployment(&#34;testpod&#34;,&#34;test123&#34;,&#34;1450793561/myapp:v1&#34;)</span>
    <span style="color:#75715e">#     # api.create_namespace(&#34;test123123&#34;)</span>
    <span style="color:#75715e"># deployment = create_deployment_object(&#34;myapp-deployment&#34;, &#34;test123123&#34;, &#34;myapp&#34;, &#34;1450793561/myapp:v2&#34;)</span>
    <span style="color:#75715e"># api.create_dep(deployment, &#34;myapp-deployment&#34;, &#34;test123123&#34;)</span>
    <span style="color:#75715e"># api.list_deployments(&#34;test123123&#34;)</span>

    <span style="color:#75715e"># api.delete_deployment(&#34;testpod&#34;, &#34;test123&#34;)</span>
</code></pre></td></tr></table>
</div>
</div>
        </div>

        
<div class="post-archive">
    <ul class="post-copyright">
        <li><strong>原文作者：</strong><a rel="author" href="https://stto_32.gitee.io/site">仨哥</a></li>
        <li style="word-break:break-all"><strong>原文链接：</strong><a href="https://stto_32.gitee.io/site/post/kubernetes-client%E4%B9%8Bpython%E8%AF%A6%E7%BB%86%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/">https://stto_32.gitee.io/site/post/kubernetes-client%E4%B9%8Bpython%E8%AF%A6%E7%BB%86%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/</a></li>
        <li><strong>版权声明：</strong>本作品采用<a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/">知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议</a>进行许可，非商业转载请注明出处（作者，原文链接），商业转载请联系作者获得授权。</li>
    </ul>
</div>
<br/>



        

<div class="post-archive">
    <h2>See Also</h2>
    <ul class="listing">
        
        <li><a href="/site/post/k8s%E7%8E%AF%E5%A2%83%E4%B8%8Bphp/">k8s环境php环境制品</a></li>
        
        <li><a href="/site/post/%E5%9F%BA%E4%BA%8E1.22.1%E7%89%88%E6%9C%AC%E7%9A%84k8s%E9%83%A8%E7%BD%B2/">【2022】最详细的k8s基于1.22.1版本部署</a></li>
        
        <li><a href="/site/post/%E5%9F%BA%E4%BA%8Enfs%E7%9A%84storageclass/">基于nfs的storageclass</a></li>
        
        <li><a href="/site/post/%E7%90%86%E8%A7%A3-Go-%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84-select-%E7%94%A8%E6%B3%95/">理解 Go 语言中的 select 用法</a></li>
        
        <li><a href="/site/post/casbin/">casbin权限管理</a></li>
        
    </ul>
</div>


        <div class="post-meta meta-tags">
            
            <ul class="clearfix">
                
                <li><a href='/site/tags/k8s'>k8s</a></li>
                
                <li><a href='/site/tags/php'>php</a></li>
                
            </ul>
            
        </div>
    </article>
    
    <div id="disqus_thread"></div>
<script type="application/javascript">
    var disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xull" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

    
    
    <div class="post bg-white">
      <script src="https://utteranc.es/client.js"
            repo= "51op/hugoblogtalks"
            issue-term="pathname"
            theme="github-light"
            crossorigin="anonymous"
            async>
      </script>
    </div>
    
</div>

                    <footer id="footer">
    <div>
        &copy; 2022 <a href="https://stto_32.gitee.io/site">Sang的博客 By 仨哥</a>
        
    </div>
    <br />
    <div>
        <div class="github-badge">
            <a href="https://gohugo.io/" target="_black" rel="nofollow"><span class="badge-subject">Powered by</span><span class="badge-value bg-blue">Hugo</span></a>
        </div>
        <div class="github-badge">
            <a href="https://github.com/flysnow-org/maupassant-hugo" target="_black"><span class="badge-subject">Theme</span><span class="badge-value bg-yellowgreen">Maupassant</span></a>
        </div>
    </div>
</footer>


    
    <script type="text/javascript">
        window.MathJax = {
            tex2jax: {
                inlineMath: [['$', '$']],
                processEscapes: true
                }
            };
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script><script src="https://cdn.bootcdn.net/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js"></script>

<a id="rocket" href="#top"></a>
<script type="text/javascript" src='/site/js/totop.js?v=0.0.0' async=""></script>



    <script type="text/javascript" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>




    <script src='/site/js/douban.js'></script>

                </div>

                <div id="secondary">
    <section class="widget">
        <form id="search" action='https://stto_32.gitee.io/site/search/' method="get" accept-charset="utf-8" target="_blank" _lpchecked="1">
      
      <input type="text" name="q" maxlength="20" placeholder="Search">
      <input type="hidden" name="sitesearch" value="https://stto_32.gitee.io/site">
      <button type="submit" class="submit icon-search"></button>
</form>
    </section>
    
    <section class="widget">
        <h3 class="widget-title">最近文章</h3>
<ul class="widget-list">
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/calico/" title="Calico网络之BGP详解">Calico网络之BGP详解</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/casbin/" title="casbin权限管理">casbin权限管理</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/cloudstack&#43;kvm/" title="cloudstack-4.1.5版本最全入门笔记【2022】">cloudstack-4.1.5版本最全入门笔记【2022】</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/glusterfs%E5%AE%89%E8%A3%85/" title="glusterfs安装">glusterfs安装</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/go%E4%B9%8Bnginx%E6%97%A5%E5%BF%97%E5%88%86%E6%9E%90/" title="go之nginx日志分析">go之nginx日志分析</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/%E4%BA%BA%E4%BA%BA%E8%83%BD%E7%9C%8B%E6%87%82%E7%9A%84Async-Go/" title="go异步">go异步</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/k8s%E7%8E%AF%E5%A2%83%E4%B8%8Bphp/" title="k8s环境php环境制品">k8s环境php环境制品</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/kubernetes-client%E4%B9%8Bpython%E8%AF%A6%E7%BB%86%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/" title="kubernetes-client之python详细接口调用">kubernetes-client之python详细接口调用</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/mac%E5%B7%A5%E5%85%B7/" title="mac工具">mac工具</a>
    </li>
    
    <li>
        <a href="https://stto_32.gitee.io/site/post/%E5%9F%BA%E4%BA%8E1.22.1%E7%89%88%E6%9C%AC%E7%9A%84k8s%E9%83%A8%E7%BD%B2/" title="【2022】最详细的k8s基于1.22.1版本部署">【2022】最详细的k8s基于1.22.1版本部署</a>
    </li>
    
</ul>
    </section>

    

    <section class="widget">
        <h3 class="widget-title"><a href='/site/categories/'>分类</a></h3>
<ul class="widget-list">
    
</ul>
    </section>

    <section class="widget">
        <h3 class="widget-title"><a href='/site/tags/'>标签</a></h3>
<div class="tagcloud">
    
    <a href="https://stto_32.gitee.io/site/tags/cloudstack/">cloudstack</a>
    
    <a href="https://stto_32.gitee.io/site/tags/k8s/">k8s</a>
    
    <a href="https://stto_32.gitee.io/site/tags/kvm/">kvm</a>
    
    <a href="https://stto_32.gitee.io/site/tags/php/">php</a>
    
    <a href="https://stto_32.gitee.io/site/tags/shell/">shell</a>
    
    <a href="https://stto_32.gitee.io/site/tags/%E4%BA%91%E5%8E%9F%E7%94%9F/">云原生</a>
    
</div>
    </section>

    
<section class="widget">
    <h3 class="widget-title">友情链接</h3>
    <ul class="widget-list">
        
        <li>
            <a target="_blank" href="https://www.cnblogs.com/xull0651/" title="Sang的博客">Sang的博客</a>
        </li>
        
        <li>
            <a target="_blank" href="https://juejin.cn/user/2796746682943495" title="掘金">掘金</a>
        </li>
        
    </ul>
</section>


    <section class="widget">
        <h3 class="widget-title">其它</h3>
        <ul class="widget-list">
            <li><a href="https://stto_32.gitee.io/site/index.xml">文章 RSS</a></li>
        </ul>
    </section>
</div>
            </div>
        </div>
    </div>
</body>

</html>